package com.zhuang.controller;

import com.github.pagehelper.PageInfo;
import com.zhuang.dto.NotificationDTO;
import com.zhuang.dto.QuestionDTO;
import com.zhuang.mapper.UserMapper;
import com.zhuang.pojo.User;
import com.zhuang.pojo.UserExample;
import com.zhuang.service.NotificationService;
import com.zhuang.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Resource
    UserMapper userMapper;
    @Autowired
    QuestionService questionService;

    @Autowired
    NotificationService notificationService;

    @GetMapping
    public String index(HttpServletRequest request,
                        HttpSession session,
                        Model model,
                        @RequestParam(name = "key", required = false) String key,
                        @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                        @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        int firstIndex = 1;
        int lastIndex = 1;
        PageInfo<QuestionDTO> result = questionService.list(currentPage, pageSize, key);
        if (result.getPages() <= 5) {
            lastIndex = result.getPages();
        } else if(result.getPages() >= 5 && currentPage <= 3) {
            lastIndex = 5;
        } else if(result.getPages() >= 5 && currentPage > 3 && currentPage < result.getPages()-2) {
            firstIndex = currentPage - 2;
            lastIndex = currentPage + 2;
        } else {
            firstIndex = result.getPages() - 4;
            lastIndex = result.getPages();
        }

        Cookie[] cookies = request.getCookies();
        List<User> users = null;
        if (cookies != null && cookies.length != 0) {
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())) {
                    if ("token".equals(cookie.getName())) {
                        String token = cookie.getValue();
                        UserExample example = new UserExample();
                        example.createCriteria().andTokenEqualTo(token);
                        users = userMapper.selectByExample(example);
                        if (users.size() != 0) {
                            model.addAttribute("user", users.get(0));
                            session.setAttribute("user", users.get(0));
                            Integer count = notificationService.count( users.get(0).getAccountId().longValue());
                            model.addAttribute("count", count);
                        }
                    }

                }
            }
        }
        model.addAttribute("key", key);
        model.addAttribute("firstIndex", firstIndex);
        model.addAttribute("lastIndex", lastIndex);
        model.addAttribute("result", result);
        return "index";
    }
}
